const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");
/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function (height) {
  let num = 0;
  const fun = (arr) => {
    if (arr.length < 3) return 0;
    let num = 0,
      h = Math.min(arr[0], arr[arr.length - 1]);
    for (let i = 1; i < arr.length - 1; i++) {
      num += h - arr[i];
    }
    return num;
  };
  while (height && height.length > 2) {
    let hI = 0;
    for (let i = 1; i < height.length; i++) {
      if (height[i] >= height[hI]) {
        num += fun(height.slice(hI, i + 1));
        hI = i;
      }
    }
    height = height.slice(hI).reverse();
  }
  return num;
};
let height = [2, 0, 2];

timeL(trap, [height]);
